//
//  HMStatusPictureView.swift
//  Faner_Weibo
//
//  Created by 范英强 on 16/2/5.
//  Copyright © 2016年 范英强. All rights reserved.
//

import UIKit
import SnapKit
private let HMStatusPictureViewItemId = "HMStatusPictureViewItemId"
//每条条目的宽高
private let itemMargin: CGFloat = 5
// 每个条目的宽高
private let itemWH = (SCREENW - itemMargin * 2 - HMStatusCellMargin * 2) / 3

class HMStatusPictureView: UICollectionView {
    var pic_urls:[HMStatusPictureInfo]?{
        didSet{
            countLabel.text = "\(pic_urls?.count ?? 0)"
            //根据图片张图 计算当前控件的大小
            let size = calcSize(pic_urls?.count ?? 0)
            //更新自己 不需要卸载 直接更新就好 不是更新其他两方的约束
            // self.sizeConstraint?.uninstall()
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(size)
            }
            reloadData()
        }
    }
    
    private func calcSize(count: Int) ->CGSize {
        
       
        let col = count == 4 ? 2 : count >= 3 ? 3: count
        let row = count == 4 ? 2 : (count - 1) / 3 + 1
        
        
        let width = CGFloat(col) * itemWH + CGFloat(col - 1) * itemMargin
        
        let height = CGFloat(row) * itemWH + CGFloat(row - 1) * itemMargin
        
        return CGSize(width: width, height: height)
    }

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        //注册
        registerClass(HMStatusPictureCell.self, forCellWithReuseIdentifier: HMStatusPictureViewItemId)
        dataSource = self
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSize(width: itemWH, height: itemWH)
        layout.minimumInteritemSpacing = itemMargin
        layout.minimumLineSpacing = itemMargin
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI(){
        addSubview(countLabel)
        
        countLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
    }
    
    private lazy var countLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 40)
}

extension HMStatusPictureView:UICollectionViewDataSource {
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        //  print(pic_urls?.count)
        return pic_urls?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(HMStatusPictureViewItemId, forIndexPath: indexPath) as! HMStatusPictureCell
        cell.pictureInfo = pic_urls![indexPath.row]
        return cell
    }
  
}

class HMStatusPictureCell: UICollectionViewCell {
    
    var pictureInfo: HMStatusPictureInfo? {
        didSet{
            if let urlString = pictureInfo?.thumbnail_pic {
//                gitImageView.hidden = !urlString.hasSuffix(".gif")
                imgView.sd_setImageWithURL(NSURL(string: urlString), placeholderImage: UIImage(named: "timeline_image_placeholder"))
                gitView.hidden = !urlString.hasSuffix(".git")
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        contentView.addSubview(imgView)
        contentView.addSubview(gitView)
        imgView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        gitView.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(contentView)
            make.bottom.equalTo(contentView)
        }
    }
    
    private lazy var imgView: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = UIViewContentMode.ScaleAspectFill
        imageView.clipsToBounds = true
        return imageView
    }()
    
    private lazy var gitView: UIImageView = UIImageView(image: UIImage(named: "timeline_image_gif"))
    
}
